package com.smrs.asn.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.util.CollectionUtils;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.smrs.asn.model.AsnSequenceModel;


public class AsnSequenceDao extends BaseDAOHibernateImpl<AsnSequenceModel, Long>{
	
	public String getAsnSequenceValue(){
		String hsql = " from AsnSequenceModel";
		//Object values = new Object[]{};
		Query query = this.createHQLQuery(hsql, new Object[]{});
		
		List list = query.list();
		AsnSequenceModel model = null;
		if(CollectionUtils.isEmpty(list)){
			model = new AsnSequenceModel();
			model.setAsnDetailSeq(1l);
			model.setAsnSequence(1l);
			save(model);		
		}else{
			model = (AsnSequenceModel)list.get(0);
		}
		model.setAsnSequence(model.getAsnSequence()+1l);
		update(model);
		Long sequenceValue = model.getAsnSequence();
		String tempSeq = "ASN0000000"+sequenceValue;
		tempSeq.substring(tempSeq.length()-10);
		return tempSeq;
	}
	
	public Long getAsnDetailSeqNextValue(){
		String hsql = " from AsnSequenceModel";
		//Object values = new Object[]{};
		Query query = this.createHQLQuery(hsql, new Object[]{});
		
		List list = query.list();
		AsnSequenceModel model = null;
		if(CollectionUtils.isEmpty(list)){
			model = new AsnSequenceModel();
			model.setAsnDetailSeq(1l);
			model.setAsnSequence(1l);
			save(model);		
		}else{
			model = (AsnSequenceModel)list.get(0);
		}
		//model.setAsnSequence(model.getAsnSequence()+1l);
		model.setAsnDetailSeq(model.getAsnDetailSeq()+1);
		update(model);
		Long sequenceValue = model.getAsnDetailSeq();
		return sequenceValue;
	}
	
	
}
